<template>
        <div class="BG">
            <div style="margin:150px auto; background-color:rgba(0,0,0,0.2); width: 400px; height: 350px; padding: 20px; border-radius: 18px">
                <div style="margin: 20px 0; text-align: center; font-size: 24px"><h2 class="loginText">登 录</h2></div>
                <el-form :model="user" :rules="rules" ref="userForm" class="demo-ruleForm">
                    <el-form-item label="账号" prop="userName">
                        <el-input placeholder="请输入账号"  size="medium" style="margin: 10px 0" prefix-icon="el-icon-user" v-model="user.userName"></eL-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input placeholder="请输入密码" size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
                    </el-form-item>
                    <div style="margin: 10px 0; text-align: right">
                        <el-button type="primary" size="small" autocomplete="off" @click="login">登录</el-button>
                        <el-button type= "warning" size="small"autocomplete="off" @click="$router.push('/register')">注册</el-button>
                    </div>
                </el-form>
            </div>
        </div>
</template>

<script>
export default {
name: "Login",
    data(){
    return{
        user:{},
        rules: {
            userName: [
                {required: true, message: '请输入用户名', trigger: 'blur'},
                {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'}
            ],
            password: [
                {required: true, message: '请输入密码', trigger: 'blur'},
                {min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur'}
            ],
        },
    }
    },
    methods:{
    login(){
            this.$refs['userForm'].validate((valid) => {
                if (valid) {
                    this.request.post("/user/login",this.user).then(res=>{
                            if (!res){
                                this.$message.error("用户名密码错误")
                            }else {
                                localStorage.setItem("userName",res.data.userName) //生命周期永久
                                sessionStorage.setItem("token",res.data.token)
                                console.log("后端传的token",res.data.token)
                                this.$router.push("/home");
                            }
                        }
                    )
                } else {
                    console.log('表单校验不合法');//不发送请求
                    return false;
                }
            });

    }
    }
}
</script>

<style scoped>

     /*登陆背景*/
     .BG {
         background-color: rgb(255, 208, 0);
         background-image: url("../images/login/background.png");
         background-size: 100% 100%;
         background-repeat: no-repeat;
         position: absolute;
         padding: 0px;
         width: 100%;
         height: auto;
         top: 0;
         left: 0;
     }

     .loginText {
         margin-bottom: 20px;
         line-height: 50px;
         text-align: center;
         font-size: 50px;
         font-weight: bolder;
         color: blanchedalmond;
         text-shadow: 2px 2px 4px #000000;
     }

</style>
